using System;
using GameFramework.Event;
using UnityEngine;
using Unity.VisualScripting;

namespace LS.UGFUnits
{
    [UnitShortTitle("Fire")]
    [UnitSurtitle("GF Event")]
    public sealed class Fire : EventUnit
    {
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput Args { get; private set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput Sender { get; private set; }
        
        protected override void Definition()
        {
            base.Definition();
            Args = ValueInput<GameEventArgs>(nameof(Args));
            Sender = ValueInput<object>(nameof(Sender), this);
            Requirement(Args, Input);
            Requirement(Sender, Input);
        }

        protected override ControlOutput In(Flow flow)
        {
            var args = flow.GetValue<GameEventArgs>(Args);
            if (args != null)
            {
                var sender = flow.GetValue<object>(Sender);
                Target?.Fire(sender, args);
            }
            else
            {
                Debug.LogError(new ArgumentException("Args is null! Can not fire framework event"));
            }
            return base.In(flow);
        }
    }
}
